﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace 简单实现注册注册登录功能
{
    internal class MySystem
    {
        private User user = new User();
        private string PATH = @"D:\User.txt";
        public void ShowMenu()
        {
            Console.WriteLine("---欢迎使用系统---");
            bool isOver = false;
            while (!isOver)
            {
                Console.WriteLine("\n请输选择操作：");
                Console.WriteLine("   1.注册账号");
                Console.WriteLine("   2.登录账号");
                Console.WriteLine("   3.退出系统");
                int input = Convert.ToInt32(Console.ReadLine());
                switch (input)
                {
                    case 1:
                        Register();
                        break;
                    case 2:
                        Login(user);
                        break;
                    case 3:
                        Console.WriteLine("感谢你的使用");
                        isOver = true;
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("请输入1~3的阿拉伯数字");
                        Console.ResetColor();
                        break;
                }
            }

        }
        public void Register()
        {
            using (FileStream fileStream = new FileStream(PATH, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                StreamReader sr = new StreamReader(fileStream);
                if (sr.Read() != -1)
                {
                    Console.WriteLine("已经注册过一次了，不需要再注册一次");
                    return;
                }
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(fileStream, user);
            }
            Console.WriteLine("请输入用户名：");
            string username = Console.ReadLine();
            Console.WriteLine("请输入密码：");
            string password = Console.ReadLine();
            user = new User(username, password);
        }
        public void Login(User user)
        {
            if(user.UserName == null && user.Password == null)
            {
                using(FileStream fileStream = new FileStream(PATH, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    try
                    {
                        user = binaryFormatter.Deserialize(fileStream) as User;
                    }
                    catch
                    {
                        Console.WriteLine("还未进行注册....");
                        return;
                    }
                    
                }
            }
            Console.WriteLine("\n请输入用户名：");
            string username = Console.ReadLine();
            Console.WriteLine("请输入密码：");
            string password = Console.ReadLine();
            User userNew = new User(username, password);

            if (user.Equals(userNew))
            {
                Console.WriteLine("登录成功");
            }
            else
            {
                Console.WriteLine("登录失败");
            }
        }
    }
}
